package com.sunmouse.sharelib;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMEmoji;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.media.UMediaObject;
import com.umeng.socialize.media.UMusic;

/**
 * Created by leiyun on 2018/5/14.
 */
public class ShareUtil {
    private Activity mAct;
    private ShareAction mShare;


    /**
     * private constructor
     *
     * @param mAct
     */
    private ShareUtil(Activity mAct) {
        this.mAct = mAct;
        this.mShare = new ShareAction(mAct);
    }

    /**
     * get instance
     */
    public static ShareUtil getInstance(Activity act){
        return new ShareUtil(act);
    }
    /**
     * add platform
     * invoke in application
     *
     * @param context
     * @param appKey
     * @param platforms
     */
    public static void initSDK(Context context, String appKey, SharePlatform... platforms) {
        if (platforms != null && platforms.length >= 1) {
//            UMShareAPI.init(context, appKey);
            UMConfigure.init(context, appKey, "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");

            for (int i = 0; i < platforms.length; ++i) {
                SharePlatform sp = platforms[i];
                if (sp.getPlatform() == SHARE_MEDIA.WEIXIN) {
                    PlatformConfig.setWeixin(sp.getAppId(), sp.getAppSecret());
                } else if (platforms[i].getPlatform() == SHARE_MEDIA.QQ) {
                    PlatformConfig.setQQZone(sp.getAppId(), sp.getAppSecret());
                } else if (platforms[i].getPlatform() == SHARE_MEDIA.ALIPAY) {
                    PlatformConfig.setAlipay(sp.getAppId());
                }
            }

        } else {
            throw new RuntimeException("should choose a platform at least");
        }
    }

    public static void setDebugMode(boolean mode) {
        UMConfigure.setLogEnabled(mode);
    }

    /**
     * set share content and platform
     *
     * @param shareContent
     * @param shareMedia
     */
    public void initShare(String shareContent, UMediaObject shareMedia) {
        if (mShare == null) {
            throw new RuntimeException("Error: ShareAction is not be initialized");
        }
        initContent(shareContent, shareMedia);
        mShare.setDisplayList(new SHARE_MEDIA[]{SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE});
    }

    /**
     * set share content
     *
     * @param shareContent
     * @param shareMedia
     */
    public void initContent(String shareContent, UMediaObject shareMedia) {
        if (mShare == null) {
            throw new RuntimeException("Error: ShareAction is not be initialized");
        }
        mShare.withText(shareContent);
        if (shareMedia instanceof UMImage) {
            mShare.withMedia((UMImage) shareMedia);
        } else if (shareMedia instanceof UMWeb) {
            mShare.withMedia((UMWeb) shareMedia);
        } else if (shareMedia instanceof UMusic) {
            mShare.withMedia((UMusic) shareMedia);
        } else if (shareMedia instanceof UMVideo) {
            mShare.withMedia((UMVideo) shareMedia);
        } else if (shareMedia instanceof UMEmoji) {
            mShare.withMedia((UMEmoji) shareMedia);
        }

    }

    /**
     * open share board
     */
    public void openShare() {
        if (mShare == null) {
            throw new RuntimeException("Error: ShareAction is not be initialized");
        }
        mShare.open();
    }

    /**
     * share in simple platform
     *
     * @param platform
     */
    public void share(SHARE_MEDIA platform) {
        if (mShare == null) {
            throw new RuntimeException("Error: ShareAction is not be initialized");
        }
        mShare.setPlatform(platform);
        mShare.share();
    }

    /**
     * handle response result
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mAct == null) {
            throw new RuntimeException("Error: Act is null");
        }
        UMShareAPI.get(mAct).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * release
     */
    public void release() {
        if (mAct == null) {
            throw new RuntimeException("Error: Act is null");
        }

        UMShareAPI.get(mAct).release();
    }


    /**
     * request permission
     */
    public static final String[] SHARE_PERMISSION = {
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,

    };

}
